﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private readonly static FeedbackRepository feedbackRepository = new FeedbackRepository();

        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        #region Feedback

        #region Submit
        
        public ActionResult LeaveFeedback()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LeaveFeedback(Feedback feedback)
        {
            if (ModelState.IsValid)
            {
                feedback.DateSubmitted = DateTime.Now;
                feedbackRepository.Add(feedback);
                feedbackRepository.Save();

                return RedirectToAction("FeedbackSubmitted");
            }
            return View();
        }
        public ActionResult FeedbackSubmitted()
        {
            return View();
        }

        #endregion

        #region Read
        [Authorize(Roles = "SiteAdmin")]
        public ActionResult ReadFeedback()
        {
            var feedbacks = feedbackRepository.GetFeedBackList(3);
            return View(feedbacks);
        }

        [Authorize(Roles = "SiteAdmin")]
        public ActionResult FeedbackDetails(int id)
        {
            var feedback = feedbackRepository.GetFeedback(id);
            
            // Mark feedback as read
            FeedbackRead(feedback);

            return View(feedback);
        }

        private void FeedbackRead(Feedback feedback)
        {
            if (!TryUpdateModel(feedback)) return;
            feedback.FeedbackRead = true;
            feedbackRepository.Save();
        }

        #endregion

        #region Delete

        public ActionResult FeedbackDelete(int id)
        {
            var feedback = feedbackRepository.GetFeedback(id);
            feedbackRepository.Delete(feedback);

            return RedirectToAction("ReadFeedback");
        }

        #endregion

        #endregion

        public ActionResult Browse()
        {
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
    }
}
